package com.wgj.android.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jiaai.xui.adapter.XRecyclerViewAdapter;
import com.jiaai.xui.adapter.XViewHolder;
import com.jiaai.xui.adapter.decoration.DividerDecoration;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.XDensityUtils;
import com.jiaai.xui.utils.http.ListHttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Version;
import com.wgj.android.entity.VersionVO;
import com.wgj.android.http.WGJAPI;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/12/13.
 */

public class VersionHistoryActivity extends AppCompatActivity {
    @BindView(R.id.historyListView)
    RecyclerView historyListView;
    private XRecyclerViewAdapter<VersionVO> versionAdapter;
    private List<VersionVO> versionList=new ArrayList<>();
    private int  selectIndex=-1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.version_history_activity);
        ButterKnife.bind(this);
        initView();
        loadData();
    }

    private void  initView(){
        versionAdapter=new XRecyclerViewAdapter<VersionVO>(historyListView,versionList,R.layout.version_history_item) {
            @Override
            protected void bindData(XViewHolder holder, VersionVO version, int i) {
                holder.setText(R.id.title,version.getTitle());
                holder.setText(R.id.versionCode,version.getV_code());
                holder.setText(R.id.updateTime, XDateUtils.date2String(new Date(version.getTime())));
                holder.setText(R.id.uploadMsg,version.getContent());
                holder.setVisible(R.id.uploadMsg,selectIndex==i);
            }
        };
        versionAdapter.setOnItemClickListener(new XRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                if(selectIndex==i){
                    selectIndex=-1;
                }else {
                    selectIndex=i;
                }
                versionAdapter.notifyDataSetChanged();
            }
        });
        historyListView.setLayoutManager(new LinearLayoutManager(this));
        historyListView.setHasFixedSize(true);
        historyListView.addItemDecoration(new DividerDecoration(R.color.divider, XDensityUtils.dp2px(1)));
        historyListView.setAdapter(versionAdapter);
    }

    private void loadData(){
        XHttp.obtain().get(WGJAPI.BASEURL.concat("version"), null, new ListHttpCallBack<VersionVO>() {
            @Override
            public void onSuccess(List<VersionVO> list) {
                  if(list!=null &&  list.size()>0){
                      versionList.clear();
                      versionList.addAll(list);
                      versionAdapter.notifyDataSetChanged();
                  }
            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("获取版本历史失败");
            }
        });
    }
}
